package com.situ.personal.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

@WebFilter(filterName = "encodingFilter",urlPatterns = {"/*"})
public class EncodingFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
		// @WebFilter(filterName = "encodingFilter",urlPatterns = {"/*"})
			// 过滤器名字，，，url执行 ，所有路径  都要 执行这个过滤器
		// 1.拿到request，，，
			// 获取getMethod的(传参方式) 必须 用HttpServletRequest，，要做一个强转
		// 2 . 获取method方式，放入String变量 method里
		// 3.比较method对象里的字符串  是不是  post
		// 4.  比较method,,如果是post方式
		 // 5. 设置 字符 成 UTF-8
			// 执行完了，往下走
		// 6. 执行doFilter ，，访问页面
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		String  method = httpServletRequest.getMethod();
		if ("post".equalsIgnoreCase(method)) {
			request.setCharacterEncoding("UTF-8");
		}
		
		chain.doFilter(request,	response);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
